//带jwt ajax
const request = (function () {
    return {
        _request (url, type, data, { isLoading, text, disTooltips }, ajaxOptions = this._defaultAjaxOptions()) {
            if (typeof $.showLoading == 'function' && isLoading) {
                $.showLoading(text);
            }
            return new Promise((resolve, reject) => {
                $.ajax({
                    url: url,
                    beforeSend: Utils.AjaxBeforeSend,
                    dataType: ajaxOptions.dataType,
                    type: type,
                    data: data,
                    async: true,
                    dataFilter: ajaxOptions.dataFilter,
                    success: (res, textStatus, xhr) => {
                        if (typeof $.showLoading == 'function' && isLoading) {
                            $.hideLoading();
                        }
                        resolve(res, textStatus, xhr);
                    },
                    error: (xhr, textStatus, error) => {
                        if (typeof $.showLoading == 'function' && isLoading) {
                            $.hideLoading();
                        }
                        if (Utils.IsNotNull(xhr.responseText)) {
                            let _json = JSON.parse(xhr.responseText);
                            //处理 请求成功但是无data返回问题
                            if (_json.isSucceed) {
                                return reject(xhr, textStatus, error);
                            }
                        }
                        disTooltips = disTooltips || false;
                        if (!disTooltips) {
                            Utils.AjaxError(xhr, textStatus, error);
                        }
                        reject(xhr, textStatus, error);
                    }
                });
            });
        },
        _defaultAjaxOptions (dataType = 'json') {
            return {
                contentType: 'application/json',
                dataType: dataType,
                async: true,
                dataFilter (data, type) {
                    if (type.toLowerCase() === 'json') {
                        const jsonData = JSON.parse(data);
                        if (jsonData.hasOwnProperty('isSucceed') && jsonData.isSucceed) {
                            return JSON.stringify(jsonData.data);
                        }
                    }
                    return data;
                }
            };
        },
        get (url, { isLoading = true, text = "数据请求中..", disTooltips = false } = {}, data) {
            return this._request(url, "GET", data, { isLoading, text, disTooltips });
        },
        post (url, data, { isLoading = true, text = "数据请求中..", ajaxOptions, disTooltips = false } = {}) {
            if (data && $.isPlainObject(data)) { //判断是否是对象
                data = JSON.stringify(data);
            }
            return this._request(url, "POST", data, { isLoading, text, disTooltips }, ajaxOptions);
        },
        put (url, data, { isLoading = true, text = "数据请求中..", ajaxOptions, disTooltips = false } = {}) {
            if (data && $.isPlainObject(data)) { //判断是否是对象
                data = JSON.stringify(data);
            }
            return this._request(url, "PUT", data, { isLoading, text, disTooltips }, ajaxOptions);
        },
        patch (url, data, { isLoading = true, text = "数据请求中..", disTooltips = false } = {}) {
            if (data && $.isPlainObject(data)) {
                data = JSON.stringify(data);
            }
            return this._request(url, "PATCH", data, { isLoading, text, disTooltips });
        },
        delete (url, data, { isLoading = true, text = "数据请求中..", disTooltips = false } = {}) {
            if (data && $.isPlainObject(data)) { //判断是否是对象
                data = JSON.stringify(data);
            }
            return this._request(url, "DELETE", data, { isLoading, text, disTooltips });
        },
        postForm (url, formData, { isLoading = true, text = "上传中...", timeout = 300000 } = {}) {
            // 严格参考 yy.html api.js:132-162 的实现
            if (typeof $.showLoading == 'function' && isLoading) {
                $.showLoading(text);
            }
            const token = Utils.GetSNAuthAT();
            return $.ajax({
                url: url,
                type: 'POST',
                data: formData,
                enctype: 'multipart/form-data',       // 参考 yy.html:139
                contentType: false,                   // 参考 yy.html:140 - 必须为 false
                processData: false,                   // 参考 yy.html:141 - 禁止处理 FormData
                cache: false,                         // 参考 yy.html:142
                async: true,                          // 参考 yy.html:143 - 默认 async 为 true
                dataType: 'json',                     // 参考 yy.html:144
                headers: {
                    Authorization: 'Bearer ' + token, // 参考 yy.html:146
                    Accept: 'application/json',       // 参考 yy.html:147
                },
                dataFilter: function (data, type) {   // 参考 yy.html:149-151
                    return data;
                },
                success: (res) => {                   // 参考 yy.html:152-154
                    if (typeof $.showLoading == 'function' && isLoading) {
                        $.hideLoading();
                    }
                },
                error: async (xhr, textStatus, errorThrown) => {  // 参考 yy.html:155-161
                    if (typeof $.showLoading == 'function' && isLoading) {
                        $.hideLoading();
                    }
                    const errorInfo = await Utils.AjaxError(xhr, textStatus, errorThrown);
                    console.error('[API Error] postForm 请求失败:', {
                        url: url,
                        status: xhr.status,
                        textStatus: textStatus,
                        errorInfo: errorInfo,
                        responseText: xhr.responseText
                    });
                }
            });
        }
    }
}());

export default request;